package com.site.web.controller.war;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.site.war.service.ISiteTowerService;
import com.site.war.service.ISiteWarningService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.site.common.annotation.Log;
import com.site.common.core.controller.BaseController;
import com.site.common.core.domain.AjaxResult;
import com.site.common.enums.BusinessType;
import com.site.war.domain.SiteWork;
import com.site.war.service.ISiteWorkService;
import com.site.common.utils.poi.ExcelUtil;
import com.site.common.core.page.TableDataInfo;

/**
 * 工地基础信息Controller
 *
 * @author site
 * @date 2023-10-10
 */
@RestController
@RequestMapping("/war/work")
public class SiteWorkController extends BaseController
{
    @Autowired
    private ISiteWorkService siteWorkService;

    @Autowired
    private ISiteTowerService siteTowerService;

    @Autowired
    private ISiteWarningService siteWarningService;

    /**
     * 查询工地基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('site:work:list')")
    @GetMapping("/list")
    public TableDataInfo list(SiteWork siteWork)
    {
        startPage();
        List<SiteWork> list = siteWorkService.selectSiteWorkList(siteWork);
        return getDataTable(list);
    }

    /**
     * 导出工地基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('site:work:export')")
    @Log(title = "工地基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SiteWork siteWork)
    {
        List<SiteWork> list = siteWorkService.selectSiteWorkList(siteWork);
        ExcelUtil<SiteWork> util = new ExcelUtil<SiteWork>(SiteWork.class);
        util.exportExcel(response, list, "工地基础信息数据");
    }

    /**
     * 获取工地基础信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('site:work:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(siteWorkService.selectSiteWorkById(id));
    }

    /**
     * 新增工地基础信息
     */
    @PreAuthorize("@ss.hasPermi('site:work:add')")
    @Log(title = "工地基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SiteWork siteWork)
    {
        return toAjax(siteWorkService.insertSiteWork(siteWork));
    }

    /**
     * 修改工地基础信息
     */
    @PreAuthorize("@ss.hasPermi('site:work:edit')")
    @Log(title = "工地基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SiteWork siteWork)
    {
        return toAjax(siteWorkService.updateSiteWork(siteWork));
    }

    /**
     * 删除工地基础信息
     */
    @PreAuthorize("@ss.hasPermi('site:work:remove')")
    @Log(title = "工地基础信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(siteWorkService.deleteSiteWorkByIds(ids));
    }

    /**
     * ZYJ
     * 项目总数历年趋势图
     * @return
     */
    @GetMapping("/getProjectNumberTrend")
    public AjaxResult getProjectNumberTrend(){
        return success(siteWorkService.getProjectNumberTrend());
    }

    /**
     * ZYJ
     * 根据街道获取信息列表
     * @param street
     * @return
     */
    @GetMapping("/getWorkListByStreet")
    public AjaxResult getWorkListByStreet(String street){
        return success(siteWorkService.getWorkListByStreet(street));
    }


    /**
     * ZYJ
     * 获取工地相关所有信息
     * @param street
     * @return
     */
    @GetMapping("/getAllMessage")
    public AjaxResult getAllMessage(String street){
        //工地信息
        List<Map<String,Object>> list = siteWorkService.getWorkBySiteNumber(street);

        if (list!=null&&list.size()>0){
            for (Map<String, Object> work:list) {
                //塔吊数量
                int towerNumber = siteTowerService.getCountBySiteNumber(String.valueOf(work.get("siteNumber")));

                //一条塔吊信息
                Map<String, Object> tower = siteTowerService.getOneTowerBySiteNumber(String.valueOf(work.get("siteNumber")));

                //隐患信息
                List<Map<String, Object>> warning = siteWarningService.getWarningBySiteNumber(String.valueOf(work.get("siteNumber")));

                work.put("towerNumber",towerNumber);
                work.put("tower",tower);
                work.put("warning",warning);
            }
        }
        return success(list);
    }


    /**
     * 大屏获取每个区预警数量
     * ZYJ
     * @return
     */
    @GetMapping("/getWarningCollect")
    public AjaxResult getWarningCollect(){
        return success(siteWorkService.getWarningCollect());
    }

    /**
     * 大屏获取每个工地预警数量
     * ZYJ
     * @return
     */
    @GetMapping("/getWorkCollect")
    public AjaxResult getWorkCollect(){
        return success(siteWorkService.getWorkCollect());
    }
}
